package com.person.jiaiplan.controller;

import com.person.jiaiplan.pojo.R;
import com.person.jiaiplan.pojo.dto.SelectConfigResDTO;
import com.person.jiaiplan.pojo.entity.AchievementLogs;
import com.person.jiaiplan.pojo.entity.ScheduleLogs;
import com.person.jiaiplan.pojo.signle.SignleSnowFlake;
import com.person.jiaiplan.service.AchievementLogsService;
import com.person.jiaiplan.service.BasicService;
import com.person.jiaiplan.service.ScheduleLogsService;
import com.person.jiaiplan.util.UploadUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

/**
 * 通用设置
 */
@AllArgsConstructor
@RestController
@RequestMapping("common")
@Validated
@Slf4j
public class CommonController {

    @Autowired
    private BasicService basicService;

    @Autowired
    private ScheduleLogsService scheduleLogsService;

    @Autowired
    private AchievementLogsService achievementLogsService;

    /**
     * 下拉框列表获取
     *
     * @param
     * @return
     */
    @GetMapping("select/config")
    public R<List<SelectConfigResDTO>> constGroupConfig() {
        return R.ok(basicService.getSelectConfig());
    }


    @PostMapping("/upload")
    public R upload(String scheduleId, @RequestParam MultipartFile file) {

        if (file.isEmpty()) {
            return R.failed("40400", "上传文件为空");
        }
        ScheduleLogs scheduleLogs = scheduleLogsService.getById(scheduleId);
        String rootPath = UploadUtils.getRootPath(scheduleLogs, null);
        // 上传文件
        UploadUtils.uploadFiles(rootPath, file);
        // 保存成果对象
        AchievementLogs achievementLogs = new AchievementLogs();
        achievementLogs.setId(SignleSnowFlake.getSnowflakeId());
        achievementLogs.setMkdir(rootPath);
        achievementLogs.setFileName(file.getOriginalFilename());
        achievementLogs.setScheduleId(scheduleId);
        achievementLogs.setCreateTime(new Date());
        achievementLogsService.saveOrUpdateAchievement(achievementLogs);
        return R.failed("40400", "上传失败");
    }

}
